/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ContactForm.java
 *
 * Created on Jun 15, 2009, 11:15:52 PM
 */

package ui;

import bus.ContactBUS;
import dto.ContactDTO;
import excom.JBoxEvent;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.MsgboxHelper;
import utils.RegexPattern;

/**
 *
 * @author Spazee
 */
public class ContactAddPanel extends AbstractFrame {

    /** Creates new form ContactForm */
    //private int contactId;
    private int contactId;
    public ContactAddPanel(int id) {
        initComponents();
        getRootPane().setDefaultButton(btnSaveAndClose);  // set default button
        setLocationRelativeTo(this);            // center the form
        contactId = id;
        if(id != -1){
            ContactBUS bus = new ContactBUS();
            ContactDTO dto;
            try {
                dto = bus.getInfo(id);
                if(dto != null)
                {
                    setTitle("Update contact");
                    btnSaveAndClose.setText("Update");
                    txtFullName.setText(dto.getFullName());
                    txtCompany.setText(dto.getCompany());
                    txtJobTitle.setText(dto.getJobTitle());
                    txtEmail.setText(dto.getEmail());
                    txtWebPage.setText(dto.getWebAddress());
                    txtDisplayAs.setText(dto.getDisplayAs());
                    txtIMAddress.setText(dto.getIMAddress());
                }
            } catch (SQLException ex) {
                Logger.getLogger(ContactAddPanel.class.getName()).log(Level.SEVERE, null, ex);
            }


        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lbCompany = new javax.swing.JLabel();
        lbJobTitle = new javax.swing.JLabel();
        txtFullName = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtCompany = new javax.swing.JTextField();
        txtJobTitle = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtWebPage = new javax.swing.JTextField();
        txtEmail = new javax.swing.JTextField();
        txtDisplayAs = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtIMAddress = new javax.swing.JTextField();
        btnSaveAndClose = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add new contact");
        setResizable(false);

        jPanel1.setName("jPanel1"); // NOI18N

        lbCompany.setText("Company :");
        lbCompany.setName("lbCompany"); // NOI18N

        lbJobTitle.setText("Job Title :");
        lbJobTitle.setName("lbJobTitle"); // NOI18N

        txtFullName.setName("txtFullName"); // NOI18N

        jLabel4.setText("Internet");
        jLabel4.setName("jLabel4"); // NOI18N

        txtCompany.setName("txtCompany"); // NOI18N

        txtJobTitle.setName("txtJobTitle"); // NOI18N

        jLabel5.setText("Display As :");
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText("Web page Address :");
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setText("IM Adress :");
        jLabel7.setName("jLabel7"); // NOI18N

        txtWebPage.setName("txtWebPage"); // NOI18N

        txtEmail.setName("txtEmail"); // NOI18N

        txtDisplayAs.setName("txtDisplayAs"); // NOI18N

        jLabel1.setText("Full Name:");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("Email:");
        jLabel2.setName("jLabel2"); // NOI18N

        txtIMAddress.setName("txtIMAddress"); // NOI18N

        btnSaveAndClose.setText("Save & Close");
        btnSaveAndClose.setName("btnSaveAndClose"); // NOI18N
        btnSaveAndClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveAndCloseActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.setName("btnCancel"); // NOI18N
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel4))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbJobTitle)
                            .addComponent(lbCompany)
                            .addComponent(jLabel1)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel2)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnCancel)
                        .addGap(83, 83, 83)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtDisplayAs)
                            .addComponent(txtEmail)
                            .addComponent(txtJobTitle)
                            .addComponent(txtCompany)
                            .addComponent(txtFullName, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                            .addComponent(txtWebPage, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtIMAddress)
                            .addComponent(btnSaveAndClose, javax.swing.GroupLayout.Alignment.TRAILING))))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(21, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbCompany)
                    .addComponent(txtCompany, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbJobTitle)
                    .addComponent(txtJobTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(7, 7, 7)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtDisplayAs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtWebPage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(txtIMAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSaveAndClose)
                    .addComponent(btnCancel)))
        );

        jMenuBar1.setName("jMenuBar1"); // NOI18N

        jMenu1.setText("File");
        jMenu1.setName("jMenu1"); // NOI18N
        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
   

    private void btnSaveAndCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveAndCloseActionPerformed

        if(txtFullName.getText().isEmpty() || txtEmail.getText().isEmpty()) {
            MsgboxHelper.showError("Name and Email cannot be empty!");
            return;
        }

        if(!RegexPattern.IsValidEmail(txtEmail.getText())) {
            MsgboxHelper.showError("Email format is not correct.");
            return;
        }

        try {
            ContactDTO dto = new ContactDTO();
            dto.setFullName(txtFullName.getText());
            dto.setCompany(txtCompany.getText());
            dto.setJobTitle(txtJobTitle.getText());
            dto.setEmail(txtEmail.getText());
            dto.setWebAddress(txtWebPage.getText());
            dto.setDisplayAs(txtDisplayAs.getText());
            dto.setIMAddress(txtIMAddress.getText());

            if(contactId != -1)// update contact
            {
                dto.setId(contactId);
                new ContactBUS().updateContact(dto);
                MsgboxHelper.inform("Contact updated!");
            }
            else// insert new
            {
                new ContactBUS().insertContact(dto);
                MsgboxHelper.inform("New contact added!");
            }
        } catch (Exception ex){
            MsgboxHelper.showError("Cannot add new contact!");
        }        
        
       
        this.dispose();
        addJBoxEvent(new JBoxEvent(evt, "addcontact"));
    }//GEN-LAST:event_btnSaveAndCloseActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.dispose();
}//GEN-LAST:event_btnCancelActionPerformed

    /**
    * @param args the command line arguments
    */
   /* public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ContactAddPanel().setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSaveAndClose;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lbCompany;
    private javax.swing.JLabel lbJobTitle;
    private javax.swing.JTextField txtCompany;
    private javax.swing.JTextField txtDisplayAs;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtFullName;
    private javax.swing.JTextField txtIMAddress;
    private javax.swing.JTextField txtJobTitle;
    private javax.swing.JTextField txtWebPage;
    // End of variables declaration//GEN-END:variables

}
